package mdt.figure;

import org.tigris.gef.presentation.Fig;
import org.tigris.gef.presentation.FigEdgePoly;
import org.tigris.gef.presentation.FigGroup;
import mdt.model.EdgeModel;
import mdt.model.IEdgeModel;

import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;

/**
 * Created by xiaobyu on 6/17/2016.
 */
public abstract class FigEdgePolyEditable extends FigEdgePoly implements KeyListener, MouseListener, FigMDT{
    private static final long serialVersionUID = 1L;
    private FigNodeEditable.TextType textType;
    private FigTextMDT figText;

    public FigEdgePolyEditable(FigNodeEditable.TextType textType) {
        this.textType = textType;
        initTextFig();
    }

    protected void initTextFig() {
        switch (textType) {
            case SINGLE_LINE:
                figText = new FigNamed(null, "  ");
                break;
            case MULTI_LINE:
                figText = new FigMultiLineText(null);
                break;
        }
    }

    public FigTextMDT getFigText() {
        return figText;
    }

    public void propertyChange(final PropertyChangeEvent pve) {
        final Object src = pve.getSource();
        final String pName = pve.getPropertyName();

        // We handle and consume editing events
        if (pName.equals("editing") && Boolean.FALSE.equals(pve.getNewValue())) {
            // resize the FigNode to accommodate the new text
            final Rectangle bbox = getBounds();
            final Dimension minSize = getMinimumSize();
            bbox.width = Math.max(bbox.width, minSize.width);
            bbox.height = Math.max(bbox.height, minSize.height);
            setBounds(bbox.x, bbox.y, bbox.width, bbox.height);
            endTrans();
        } else if (pName.equals("editing") && Boolean.TRUE.equals(pve.getNewValue())) {
            // do nothing

        } else {
            super.propertyChange(pve);
        }
    }

    public Dimension getMinimumSize() {
        return new Dimension(Math.max(50, (int) figText.getMinimumSize().getWidth()), 20);
    }

    public void mouseClicked(MouseEvent me) {
        if (me.isConsumed()) {
            return;
        }
        if (me.getClickCount() >= 2 && !(me.isPopupTrigger() || me.getModifiers() == InputEvent.BUTTON3_MASK)
                && getOwner() != null) {
            Rectangle r = new Rectangle(me.getX() - 2, me.getY() - 2, 4, 4);
            Fig f = hitFig(r);
            if (f instanceof MouseListener && f.isVisible()) {
                ((MouseListener) f).mouseClicked(me);
            } else if (f instanceof FigGroup && f.isVisible()) {
                // this enables direct text editing for sub figs of a
                // FigGroup object:
                Fig f2 = ((FigGroup) f).hitFig(r);
                if (f2 instanceof MouseListener) {
                    ((MouseListener) f2).mouseClicked(me);
                }
            }
        }
    }

    public void keyPressed(KeyEvent ke) {
        if (ke.isConsumed() || getOwner() == null) {
            return;
        }
        figText.keyPressed(ke);
    }

    public void keyReleased(KeyEvent ke) {
        if (ke.isConsumed() || getOwner() == null) {
            return;
        }
        figText.keyReleased(ke);
    }

    public void keyTyped(KeyEvent ke) {
        if (ke.isConsumed() || getOwner() == null) {
            return;
        }
        figText.keyTyped(ke);
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }


    public void translateCloned(int dx, int dy) {
        translate(dx, dy);
    }


    public static enum TextType {
        SINGLE_LINE,
        MULTI_LINE
    }

    public Object clone() {
        FigEdgePolyEditable fig = cloneFig();
        fig.setOwner(cloneOwner());

        return fig;
    }

    public FigEdgePolyEditable cloneFig() {
        FigEdgePolyEditable fig = (FigEdgePolyEditable) super.clone();
        fig.figText = (FigTextMDT) figText.clone();

        return fig;
    }

    public IEdgeModel cloneOwner() {
        if (getOwner() != null) {
            return (IEdgeModel)((EdgeModel)getOwner()).clone();
        }

        return null;
    }
}
